package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.domain.dto.ArticleDTO;
import com.vueapi.service.HjArticleService;
import com.vueapi.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Classname AdminBlogController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/22 11:18
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/blog")
public class AdminBlogController {


    @Autowired
    private HjArticleService articleService;

    @GetMapping("/list")
    public ResultUtils list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "") String title,
                            @RequestParam(required = false, defaultValue = "") Integer categoryId,
                            @RequestParam(required = false, defaultValue = "") Integer isPublish) {
        return articleService.listPage(pageNum, pageSize, title, categoryId, isPublish);
    }

    @OperationLog(name = "添加博客")
    @PostMapping("/add")
    public ResultUtils add(@RequestBody ArticleDTO articleDto) {
        return articleService.addBlog(articleDto);
    }

    @OperationLog(name = "修改博客置顶状态")
    @GetMapping("/top/{id}")
    public ResultUtils top(@PathVariable("id") Integer id) {
        return articleService.top(id);
    }

    @OperationLog(name = "修改博客发布状态")
    @GetMapping("/changePublish/{id}")
    public ResultUtils changePublish(@PathVariable("id") Integer id) {
        return articleService.changePublish(id);
    }

    @OperationLog(name = "修改博客评论区是否开启关闭")
    @GetMapping("/changeComment/{id}")
    public ResultUtils changeComment(@PathVariable("id") Integer id) {
        return articleService.changeComment(id);
    }

    @OperationLog(name = "根据ID获取博客信息")
    @GetMapping("/getById/{id}")
    public ResultUtils getById(@PathVariable("id") Integer id) {
        return articleService.getArticleById(id);
    }

    @OperationLog(name = "修改博客")
    @PutMapping("/edit")
    public ResultUtils edit(@RequestBody ArticleDTO articleDTO) {
        return articleService.updateBlog(articleDTO);
    }

    @OperationLog(name = "删除博客")
    @DeleteMapping("/delete")
    public ResultUtils delete(@RequestParam String ids) {
        return articleService.deleteBlogs(ids);
    }

    @GetMapping("/getHotBlog")
    public ResultUtils getHotBlog() {
        // 先查询访问量最多的前六个文章
        // 然后再用 stream 流封装为 VO 返回
        return ResultUtils.success(articleService.getHotBlog());
    }
}
